#include "pch.h"
#include "ADProgram.h"
#include "VertexShader.h"
#include "FragmentShader.h"
#include "AssemblyDemonstration.h"

#define GLSL(src) "#version 330\n" #src

ADProgram::ADProgram()
{
	SetupShaders();
}

bool ADProgram::SetupShaders()
{
	if (!SetupVertexShader())
	{
		return false;
	}
	else if (!SetupFragmentShader())
	{
		return false;
	}

	Bind();

	if (GetStatus() == false)
	{
		AfxMessageBox(CString("Shaders compile: ") + CString(GetStatusMessage()));
		theApp.PostThreadMessage(WM_QUIT, 0, 0);
		return false;
	}
	return true;
}

bool ADProgram::SetupFragmentShader()
{
	FragmentShader fragmentShader;
	fragmentShader.SetSource(GLSL(
		out vec4 FragColor;
	in vec2 TexCoords;

	uniform sampler2D texture_diffuse1;

	void main()
	{
		FragColor = texture(texture_diffuse1, TexCoords);
	}
	));


	if (fragmentShader.GetStatus() == false)
	{
		AfxMessageBox(CString("Fragment shader: ") + CString(fragmentShader.GetStatusMessage()));
		theApp.PostThreadMessage(WM_QUIT, 0, 0);
		return false;
	}

	Attach(fragmentShader);

	return true;
}

bool ADProgram::SetupVertexShader()
{
	VertexShader vertexShader;

	vertexShader.SetSource(GLSL(
		layout(location = 0) in vec3 aPos;
	layout(location = 1) in vec3 aNormal;
	layout(location = 2) in vec2 aTexCoords;

	out vec2 TexCoords;

	uniform mat4 model;
	uniform mat4 view;
	uniform mat4 projection;

	void main()
	{
		TexCoords = aTexCoords;
		gl_Position = projection * view * model * vec4(aPos, 1.0);
	}
	));

	if (vertexShader.GetStatus() == false)
	{
		AfxMessageBox(CString("Vertex shader: ") + CString(vertexShader.GetStatusMessage()));
		theApp.PostThreadMessage(WM_QUIT, 0, 0);
		return false;
	}

	Attach(vertexShader);

	return true;
}